package lc;
import java.util.*;
import org.junit.*;
public class Ex862 {
    class Solution {
        public int shortestSubarray(int[] nums, int k) {
            int n = nums.length, res = Integer.MAX_VALUE;
            int sum = 0;
            Map<Integer, Integer> map = new HashMap<>();
            map.put(0, -1);
            for (int i = 0; i < n; i++) {
                sum += nums[i];
                int tar = sum - k;
                Integer last = map.get(tar);
                if (last != null) {
                    res = Math.min(i - last, res);
                }
                map.put(sum, i); //覆盖之前的
            }
            return res == Integer.MAX_VALUE ? -1 : res;
    }
}

    @Test
    public void test() {
        Solution s = new Solution();
        int[] nums = new int[]{48,99,37,4,-31};
        System.out.println(s.shortestSubarray(nums, 140));
    }
}
